/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;


import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dao.SyncTaskDao;
import com.sigmatrix.h5.dto.SyncTaskData;
import com.sigmatrix.h5.entity.SyncTask;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.SyncTaskService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.web.vo.SyncTaskVo;


/**
 * @ClassName: SyncTaskServiceImpl
 * @Description: 异步任务业务层接口
 * @author youjun @date 2017-11-27 16:04:46
 */
@Service(value = "syncTaskService")
public class SyncTaskServiceImpl extends BaseServiceImpl<SyncTaskData> implements SyncTaskService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SyncTaskServiceImpl.class);
	
	/** 异步任务数据库操作接口 */
	@Autowired
	private transient SyncTaskDao syncTaskDao;

	@Override
	public Response findById(final Integer id, final String enterpriseCode) throws ServiceException {
		if (id == null) {
			throw new ServiceException("请指定任务主键");
		}

		try {
			final SyncTaskData data = new SyncTaskData();
			final SyncTask syncTask = syncTaskDao.findById(id, enterpriseCode);
			if (syncTask != null) {
				BeanUtils.copyProperties(syncTask, data);
				try {
					final JSONObject obj = JSONObject.parseObject(syncTask.getResponseResult());
					data.setResponseResult(obj);
				} catch (Exception e) {
					LOGGER.error("转换json对象出错", e);
				}
				return new Response(ResponseEnum.SUCCESS, data);
			}
			return new Response(ResponseEnum.NO_DATA,data);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	/**(non-Javadoc)
	 * @throws SigmatrixServiceException 
	 *@Title: save
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.SyncTaskService#save(com.sigmatrix.sc.handheld.inter.web.vo.SyncTaskVo)
	 *@author youjun
	 *@date 2017年11月27日 下午6:21:05
	 */
	@Override
	public int save(final SyncTaskVo syncTaskVo) throws ServiceException {
		if(syncTaskVo==null 
				|| syncTaskVo.getEnterpriseCode()==null) {
			throw new ServiceException();
		}
		final String enterpriseCode = syncTaskVo.getEnterpriseCode();
		final SyncTask syncTask = new SyncTask();
		BeanUtils.copyProperties(syncTaskVo, syncTask);
		try {
			return syncTaskDao.save(syncTask, enterpriseCode);
		} catch (DaoException e) {
			throw new ServiceException("save fail", e);
		}
	}

	/**
	 * (non-Javadoc)
	 *@Title: updateByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.SyncTaskService#updateByCondition(java.lang.String, int, com.sigmatrix.sc.handheld.inter.entity.SyncTask)
	 *@author youjun
	 *@date 2017年11月27日 下午7:59:22
	 */
	@Override
	public void updateByCondition(String enterpriseCode, int syncTaskId, SyncTask updateSyncTask)
			throws ServiceException {
		
		try {
			final SyncTask syncTask = syncTaskDao.findById(syncTaskId, enterpriseCode);
			if(syncTask!=null) {
				if(updateSyncTask.getIsSuccess()!=null) {
					syncTask.setIsSuccess(updateSyncTask.getIsSuccess());
				}
				if(updateSyncTask.getResponseCode()!=null) {
					syncTask.setResponseCode(updateSyncTask.getResponseCode());
				}
				if(updateSyncTask.getResponseMsg()!=null) {
					syncTask.setResponseMsg(updateSyncTask.getResponseMsg());
				}
				if(updateSyncTask.getResponseResult()!=null) {
					syncTask.setResponseResult(updateSyncTask.getResponseResult());
				}
				if(updateSyncTask.getStatus()!=null) {
					syncTask.setStatus(updateSyncTask.getStatus());
				}
				if(updateSyncTask.getSyncErrorMsg()!=null) {
					syncTask.setSyncErrorMsg(updateSyncTask.getSyncErrorMsg());
				}
				if(updateSyncTask.getTypeSign()!=null) {
					syncTask.setTypeSign(updateSyncTask.getTypeSign());
				}
				syncTask.setUpdateTime(new Date());
			}
			syncTaskDao.update(syncTask, enterpriseCode);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	/**(non-Javadoc)
	 *@Title: updateStatus
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.SyncTaskService#updateStatus(java.lang.String, int, short)
	 *@author youjun
	 *@date 2017年11月27日 下午8:11:03
	 */
	@Override
	public void updateStatus(String enterpriseCode, int syncTaskId, short syncTaskStatusHandleIng)
			throws ServiceException {

		try{
			syncTaskDao.updateStatus(enterpriseCode, syncTaskId, syncTaskStatusHandleIng);
		}catch(DaoException e){
			throw new ServiceException(e);
		}
	}

	/**(non-Javadoc)
	 *@Title: updateSyncErrorMsg
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.SyncTaskService#updateSyncErrorMsg(java.lang.String, java.lang.Integer, java.lang.String)
	 *@author youjun
	 *@date 2017年11月30日 上午10:29:58
	 */
	@Override
	public void updateSyncErrorMsg(String enterpriseCode, Integer syncTaskId, String syncErrorMsg) {
		
		final SyncTask updateSyncTask = new SyncTask();
		updateSyncTask.setSyncErrorMsg(syncErrorMsg);
		try {
			updateByCondition(enterpriseCode, syncTaskId, updateSyncTask );
		} catch (ServiceException e) {
			LOGGER.error("记录异常任务的异步操作消息失败", e);
		}
	}

}
